/**
 * @Author: LiMeng
 * @Date: 2017/4/24
 */
var GLOBAL = {
    ID: $.getQueryParam("id"),
    ACTION_URL: '',
    CONTRACTOR_ID: ''
};
var getSelectedContractorAudit = function (data) {
    data = data[0];
    $('#contractorAuditName').textbox('setValue', data.contractorName);
    $('#contractorAuditId').val(data.id);
    $('#contractorTypeName').textbox('setValue', data.contractorTypeName);
    $('#contractorTypeId').val(data.contractorTypeId);
    GLOBAL.CONTRACTOR_ID = data.contractorId;
};
var getSelectedContractorStaff = function (data, target) {
    if (target === null || target === '') {
        return;
    }
    data = data[0];

    $('#' + target + 'Name').textbox('setValue', data.name);
    $('#' + target + 'Id').val(data.id);
    getManagerCertByPersonId(data.id, target);
};
var getSelectedProject = function (data) {
    data = data[0];
    $('#projectName').textbox('setValue', data.name);
    $('#projectId').val(data.id);
};
var openSelectContractorStaff = function (title, target) {
    title = title || '请选择承包商人员人员';
    target = target || '';
    if (GLOBAL.CONTRACTOR_ID === '') {
        layer.alert('请先选择承包商！');
        return;
    }
    WindowManager.openLayer({
        type: 2,
        title: title,
        area: ['700px', '400px'],
        content: ROOT_PATH + '/contractor/contractorStaff/simpleList.shtml?contractorId=' + GLOBAL.CONTRACTOR_ID,
        callBack: function (data) {
            getSelectedContractorStaff(data, target);
        }
    });
};
var clearTextboxRelatedContractorId = function () {
    $('#managerName').textbox('setValue', '');
    $('#managerId').val('');
    $('#certCode_manager').textbox('setValue', '');
    $('#validStartDate_manager').datebox('setValue', '');
    $('#validEndDate_manager').datebox('setValue', '');
};
var getManagerCertByPersonId = function (personId, target) {
    personId = personId || '';
    if (personId === '') {
        return;
    }
    $.ajax({
        type: 'GET',
        dataType: 'json',
        url: ROOT_PATH + '/contractor/managerCert/selectByPersonId.shtml?personId=' + personId,
        success: function (result) {
            if (result.success) {
                var data = result.data;
                $('#certCode_' + target).textbox('setValue', data.certCode ? data.certCode : '');
                $('#validStartDate_' + target).datebox('setValue', data.validStartDate ? data.validStartDate : '');
                $('#validEndDate_' + target).datebox('setValue', data.validEndDate ? data.validEndDate : '');
            } else {
                $('#certCode_' + target).textbox('setValue', '');
                $('#validStartDate_' + target).datebox('setValue', '');
                $('#validEndDate_' + target).datebox('setValue', '');
            }
        }
    });
};
var toggleFieldDisableState = function (state) {
    $('#contractorAuditName').textbox(state);
    $('#validStartDate_manager').datebox(state);
    $('#validEndDate_manager').datebox(state);
    $('#applicantDate').datebox(state);
    $('#contractorTypeName').textbox(state);
    $('#projectName').textbox(state);
    $('#managerName').textbox(state);
    $('#certCode_manager').textbox(state);
};
var hideButtons = function () {
    $('.menu-right').addClass('hide');
};
var setElementWithState = function () {
    var state = $.getQueryParam('state');
    switch (state) {
        case 'copy':
            GLOBAL.ACTION_URL = ROOT_PATH + '/contractor/contractorAudit/add.shtml';
            break;
        case 'edit':
            GLOBAL.ACTION_URL = ROOT_PATH + '/contractor/contractorAudit/edit.shtml';
            break;
        default:
            toggleFieldDisableState('disable');
            hideButtons();
    }
};
var initPageState = function () {
    GLOBAL.CONTRACTOR_ID = $('#contractorId').val();
};
$(function () {
    initPageState();

    $('#contractorAuditName').textbox({
        required: true,
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                var me = this;
                WindowManager.openLayer({
                    type: 2,
                    title: '选择承包商',
                    area: ['700px', '400px'],
                    content: ROOT_PATH + '/contractor/contractorAudit/auditedList.shtml',
                    callBack: getSelectedContractorAudit
                });
            }
        }],
        editable: false,
        onChange: function (newVal, oldVal) {
            if (newVal !== oldVal) {
                clearTextboxRelatedContractorId();
            }
        }
    });

    $('#projectName').textbox({
        required: true,
        editable: false,
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                var me = this;
                WindowManager.openLayer({
                    type: 2,
                    title: '选择项目',
                    area: ['800px', '300px'],
                    content: ROOT_PATH + '/threeSimultaneities/projectInfo/projectQueryList.shtml',
                    callBack: getSelectedProject
                });
            }
        }]
    });

    $('#managerName').textbox({
        required: true,
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                openSelectContractorStaff('选择承包商项目经理', 'manager');
            }
        }],
        editable: false
    });

    setElementWithState();

    $('#btn_save').on('click', function () {
        $.messager.progress();	// 显示进度条

        $('#form1').form('submit', {
            url: GLOBAL.ACTION_URL,
            onSubmit: function (param) {
                var isValid = $(this).form('validate');
                if (!isValid) {
                    $.messager.progress('close');	// 如果表单是无效的则隐藏进度条
                }
                return isValid;	// 返回false终止表单提交
            },
            success: function (result) {
                result = $.parseJSON(result);
                if (result.success) {
                    $.messager.progress('close');	// 如果提交成功则隐藏进度条
                    layer.msg(result.message, {time: 1000, icon: 1}, function () {
                        $.getIframeWindow().$('#dg1').datagrid('load');
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                    });
                } else {
                    $.messager.progress('close');	// 如果提交成功则隐藏进度条
                    layer.msg(result.message, {icon: 2});
                }
            }
        });
    });
});